﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using InstantNaukriDAL;
using System.Collections.Generic;

public partial class JobseekerProfile_EditGeneralInfo : System.Web.UI.Page
{
    int ProfileId = 0, UserId = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["UserId"] == null)
                Response.Redirect("Default.aspx");
            string roleid = "";
            clsUserLoginDAL objclsUserLoginDAL = new clsUserLoginDAL();
            if (Request.QueryString["roleid"] != null)
                roleid = clsEncryptionDecryption.Decrypt(Request.QueryString["roleid"].ToString());
            if (roleid == "4")
            {
                IN_TB_USER objIN_TB_USER = new IN_TB_USER();
                if (Session["UserId"] != null)
                    objIN_TB_USER = objclsUserLoginDAL.GetUserByID(Convert.ToInt32(Session["UserId"].ToString()));
                if (objIN_TB_USER != null)
                {
                    Session["UserId"] = objIN_TB_USER.ID;
                    dvAutority.Visible = true;
                    Fillddls();
                    FillGeneralInfo(objIN_TB_USER.ID);
                }
            }
        }
    }

    private void Fillddls()
    {
        try
        {
            // City
            
            clsCity objclsCity = new clsCity();
            List<IN_TB_STATE> objIN_TB_STATE = new List<IN_TB_STATE>();
            objIN_TB_STATE = objclsCity.GetAllStates();
            ddlState.DataTextField = "STATE";
            ddlState.DataValueField = "ID";
            ddlState.DataBind();
            ddlState.Items.Insert(0, new ListItem("Select", "0"));
            
            
        }
        catch (Exception ex)
        {
        }
    }

    protected void FillGeneralInfo(int UserId)
    {
        try
        {
            clsProfileDAL objclsProfileDAL = new clsProfileDAL();
            IN_TB_PROFILE objIN_TB_PROFILE = new IN_TB_PROFILE();
            objIN_TB_PROFILE = objclsProfileDAL.GetProfileByUserId(UserId);
            if (objIN_TB_PROFILE != null)
            {
                ViewState["ProfileId"] = objIN_TB_PROFILE.ID;
                txtFirstName.Text = objIN_TB_PROFILE.F_NAME;
                txtMiddleName.Text = objIN_TB_PROFILE.M_NAME;
                txtLastName.Text = objIN_TB_PROFILE.L_NAME;
                DateTime dtdob = (DateTime)objIN_TB_PROFILE.DOB;
                ddlCity.SelectedValue = Convert.ToString(objIN_TB_PROFILE.CITY);

                txtPhoneNumber.Text = objIN_TB_PROFILE.PHONE;
                txtMobileNumber.Text = objIN_TB_PROFILE.MOBILE;
                txtEmail.Text = objIN_TB_PROFILE.EMAIL;
            }
        }
        catch (Exception)
        {

            throw;
        }
    }

    protected void imgDob_Click(object sender, ImageClickEventArgs e)
    {
        //try
        //{
        //    clDob.Visible = true;
        //}
        //catch (Exception)
        //{

        //    throw;
        //}
    }
    protected void clDob_SelectionChanged(object sender, EventArgs e)
    {
        //try
        //{
        //    txtDOB.Text = clDob.SelectedDate.ToString("dd-MMM-yyyy");
        //    clDob.Visible = false;
        //}
        //catch (Exception)
        //{

        //    throw;
        //}

    }

    protected void btnupdateGeneralInfo_Click(object sender, EventArgs e)
    {
        try
        {


            clsUserLoginDAL objclsUserLoginDAL = new clsUserLoginDAL();
            //Saving User
            IN_TB_USER objIN_TB_USER = new IN_TB_USER();

            // Saving User Profile
            if (ViewState["ProfileId"] != null)
            {
                clsProfileDAL objclsProfileDAL = new clsProfileDAL();
                IN_TB_PROFILE objIN_TB_PROFILE = new IN_TB_PROFILE();
                int pid = Convert.ToInt32(ViewState["ProfileId"].ToString());
                objIN_TB_PROFILE = objclsProfileDAL.GetProfileById(pid);
                objIN_TB_PROFILE.F_NAME = txtFirstName.Text;
                objIN_TB_PROFILE.M_NAME = txtMiddleName.Text;
                objIN_TB_PROFILE.L_NAME = txtLastName.Text;
                objIN_TB_PROFILE.CITY = Convert.ToInt32(ddlCity.SelectedValue);
                clsCity objclsCity = new clsCity();
                IN_TB_CITY objIN_TB_CITY = new IN_TB_CITY();
                objIN_TB_CITY = objclsCity.GetCityById(Convert.ToInt32(ddlCity.SelectedValue));
                if (objIN_TB_CITY != null)
                    ddlState.Text = Convert.ToInt32(objIN_TB_CITY.STATE_ID).ToString();
                objIN_TB_PROFILE.PHONE = txtPhoneNumber.Text;
                objIN_TB_PROFILE.MOBILE = txtMobileNumber.Text;
                objIN_TB_PROFILE.EMAIL = txtEmail.Text;
                
                if (Session["UserId"] != null)
                    objIN_TB_PROFILE.USER_ID = Convert.ToInt32(Session["UserId"]);
                int profieleid = objclsProfileDAL.SaveProfile(objIN_TB_PROFILE);
            }

            if (Request.QueryString["roleid"] != null && Session["UserId"] != null)
            {
                Response.Redirect("JobseekerPrieview.aspx?roleid=" + Request.QueryString["roleid"].ToString());
            }
            else
            {
                Session["RoleId"] = null;
                Response.Redirect("JobSeekerLogin.aspx");
            }
        }
        catch (Exception)
        {

            throw;
        }
    }

    protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
    {
        clsCity objclsCity = new clsCity();
        List<IN_TB_CITY> objIN_TB_CITY = new List<IN_TB_CITY>();
        objIN_TB_CITY = objclsCity.GetAllCitiesbyState(Convert.ToInt32(ddlState.SelectedValue));
        ddlCity.DataTextField = "CITY";
        ddlCity.DataValueField = "ID";
        ddlCity.DataBind();
        ddlCity.Items.Insert(0, new ListItem("Select", "0"));
    }
}
